#ifndef URL_DECODE_H
#define URL_DECODE_H

#include <string>
//convert URL to UTF-8 charaset

uint8_t hex2bin(char c)
{
    switch (c)
    {
    case '0':
        return 0x0;
        break;
    case '1':
        return 0x1;
        break;
    case '2':
        return 0x2;
        break;
    case '3':
        return 0x3;
        break;
    case '4':
        return 0x4;
        break;
    case '5':
        return 0x5;
        break;
    case '6':
        return 0x6;
        break;
    case '7':
        return 0x7;
        break;
    case '8':
        return 0x8;
        break;
    case '9':
        return 0x9;
        break;
    case 'A':
        return 0xA;
        break;
    case 'B':
        return 0xB;
        break;
    case 'C':
        return 0xC;
        break;
    case 'D':
        return 0xD;
        break;
    case 'E':
        return 0xE;
        break;
    case 'F':
        return 0xF;
        break;
    default:
        return 0x0;
        break;
    }
}

std::string URLDecode(std::string url)
{
    auto charPtr=url.begin();
    std::string outputString={};
    for(;charPtr!=url.end();charPtr++)
    {
        if(*charPtr=='%')
        {
            charPtr++;
            uint8_t aCHAR;
            aCHAR=hex2bin(*charPtr);
            aCHAR<<=4;
            charPtr++;
            aCHAR=hex2bin(*charPtr) | aCHAR;
            outputString+=aCHAR;
        }
        else
        {
            outputString+=*charPtr;
        }
    }
    return outputString;
}

#endif